// 自动生成模板EpidemicRelatedStaff
package epidemic

import (
	"encoding/base64"
	"gitee.com/baal1990/epidemic/global"
	"gitee.com/baal1990/epidemic/utils/crypto"
	"gorm.io/gorm"
	"reflect"
	"time"
)

// EpidemicRelatedStaff 结构体
type EpidemicRelatedStaff struct {
	global.GVA_MODEL
	Mobile                string     `json:"mobile" form:"mobile" gorm:"column:mobile;comment:手机号;size:20;"`
	Name                  string     `json:"name" form:"name" gorm:"column:name;comment:姓名;size:50;" dfitcencode:"aes"`
	Company               string     `json:"company" form:"company" gorm:"column:company;comment:公司：字典值;size:50;"`
	Department            string     `json:"department" form:"department" gorm:"column:department;comment:部门：字典值;size:50;"`
	Organization          string     `json:"organization" form:"organization" gorm:"column:organization;comment:编制：字典值;size:50;"`
	Location              string     `json:"location" form:"location" gorm:"column:location;comment:工作地点：字典值;size:50;"`
	AppletReport          string     `json:"appletReport" form:"appletReport" gorm:"column:applet_report;comment:是否上传小程序：字典值;size:50;"`
	IsolationType         string     `json:"isolationType" form:"isolationType" gorm:"column:isolation_type;comment:隔离情况分类：字典值;size:50;"`
	Region                string     `json:"region" form:"region" gorm:"column:region;comment:居住地址-省/市/区：行政编码值：010/01/01;size:100;" dfitcencode:"aes"`
	RegionName            string     `json:"regionName" form:"regionName" gorm:"column:region_name;comment:居住地址-省/市/区;size:200;" dfitcencode:"aes"`
	ResidentialAddress    string     `json:"ResidentialAddress" form:"ResidentialAddress" gorm:"column:residential_address;comment:居住地址-详细地址：精确到门牌号;size:100;" dfitcencode:"aes"`
	Community             string     `json:"community" form:"community" gorm:"column:community;comment:所属社区;size:200;"`
	CommunityTel          string     `json:"communityTel" form:"communityTel" gorm:"column:community_tel;comment:社区电话;size:32;"`
	Street                string     `json:"street" form:"street" gorm:"column:street;comment:所属街道;size:200;"`
	Description           string     `json:"description" form:"description" gorm:"column:description;comment:情况描述;size:200;"`
	CommunityRequirements string     `json:"communityRequirements" form:"communityRequirements" gorm:"column:community_requirements;comment:情况描述;size:200;"`
	NucleicAcidRecord     string     `json:"nucleicAcidRecord" form:"nucleicAcidRecord" gorm:"column:nucleic_acid_record;comment:核酸记录;size:200;"`
	TemperatureRecord     string     `json:"temperatureRecord" form:"temperatureRecord" gorm:"column:temperature_record;comment:体温记录;size:200;"`
	HomeBeginDate         *time.Time `json:"homeBeginDate" form:"homeBeginDate" gorm:"column:home_begin_date;comment:居家开始时间;"`
	ReturnDate            *time.Time `json:"returnDate" form:"returnDate" gorm:"column:return_date;comment:返岗时间;"`
	CompanyOaDate         *time.Time `json:"companyOaDate" form:"companyOaDate" gorm:"column:company_oa_date;comment:公司OA提交时间;"`
	ReportDate            *time.Time `json:"reportDate" form:"reportDate" gorm:"column:report_date;comment:填报日期;"`
	ReportedBy            string     `json:"reportedBy" form:"reportedBy" gorm:"column:reported_by;comment:填报人;size:50;"`
	Details               string     `json:"details" form:"details" gorm:"-"`
}

// TableName EpidemicRelatedStaff 表名
func (EpidemicRelatedStaff) TableName() string {
	return "epidemic_related_staff"
}

// 保存前加密数据
func (diary *EpidemicRelatedStaff) BeforeSave(db *gorm.DB) (err error) {
	v := reflect.ValueOf(diary).Elem() // the struct variable
	for i := 0; i < v.NumField(); i++ {
		fieldInfo := v.Type().Field(i) // a reflect.StructField
		tag := fieldInfo.Tag           // a reflect.StructTag
		name := tag.Get("dfitcencode")
		if name == "aes" {
			value := v.FieldByName(fieldInfo.Name).String()
			//fmt.Println("xxxx", name)
			//fmt.Println("fieldInfo.Name:", fieldInfo.Name)
			//fmt.Println("fieldInfo.Value:", value)
			rs := crypto.MysqlAESEncrypt([]byte(value), []byte(global.ENCRYPTKEY_AES))
			dataByte := base64.StdEncoding.EncodeToString(rs) // 不可用于http传输，否则使用URLEncoding
			v.FieldByName(fieldInfo.Name).Set(reflect.ValueOf(string(dataByte)))
		}
	}
	return
}

// 返回前解密数据
func (diary *EpidemicRelatedStaff) AfterFind(tx *gorm.DB) (err error) {
	v := reflect.ValueOf(diary).Elem() // the struct variable
	for i := 0; i < v.NumField(); i++ {
		fieldInfo := v.Type().Field(i) // a reflect.StructField
		tag := fieldInfo.Tag           // a reflect.StructTag
		name := tag.Get("dfitcencode")
		if name == "aes" {
			value := v.FieldByName(fieldInfo.Name).String()
			//fmt.Println("xxxx", name)
			//fmt.Println("fieldInfo.Name:", fieldInfo.Name)
			//fmt.Println("fieldInfo.Value:", value)
			dataByte, err := base64.StdEncoding.DecodeString(value) // 不可用于http传输，否则使用URLEncoding
			if err != nil {
				continue
			}
			rs := crypto.MysqlAESDecrypt(dataByte, []byte(global.ENCRYPTKEY_AES))
			v.FieldByName(fieldInfo.Name).Set(reflect.ValueOf(string(rs)))
		}
	}
	return
}

// 体温记录
type TemperatureRecords struct {
	Records []TemperatureByDate `json:"temperatureRecords"`
}

// 每日体温记录
type TemperatureByDate struct {
	Date         string        `json:"date"`
	Temperatures []Temperature `json:"temperatures"`
}

// 体温记录，时段（上午、下午）、温度
type Temperature struct {
	Interval    string `json:"interval"`
	Temperature string `json:"temperature"`
}

// 核酸记录
type CovidTestingRecords struct {
	Records []CovidTestingByDate `json:"covidTesting"`
}

// 每日核酸记录
type CovidTestingByDate struct {
	Date   string `json:"date"`
	Result string `json:"result"`
}
